BinAST
or
How fast can JavaScript start?
David Teller (Yoric), Mozilla
Joint work:
- Mozilla (SpiderMonkey team, community)
- Facebook (WebPerf team)
- Bloomberg
- CloudFlare
- “53% of visits are abandoned if a mobile site takes more than 3 seconds to load” (source: DoubleClick)
–
- “Apps became interactive in 8 seconds on desktop (using cable) and 16 seconds on mobile (Moto G4 over 3G)”(median value, source: Addy Osmani, Google)
What’s the problem?
- Lots of code.
- Code slows you down, even when you don’t execute it.
Parsing
![]()
JS startup pipeline
![]()
JS startup pipeline
![]()
+ Optimizations
![]()
Contrast with ~native
![]()
Hello, BAST
![]()
How we test it! (*)
- Replace
uglify with binjs_encode.
- Replace
text/javascript with application/javascript-binast.
- Done.
(*) Not ready for prime-time.
Fixing parsing
Beyond IIFE
Parsing is slow, because
- Tokens are complicated.
- Strings are complicated.
eval.
SyntaxError.
- Closures.
So…
- Simplify tokens, strings.
- Pre-process
eval, SyntaxError, closures.
Results
- Time spent parsing + verifying: -30%.
- Further optimizations coming :)
Fixing download
Beyond minification
- Strings, identifiers, properties are repeated.
- Many repeats across libraries.
- Code has patterns!
- String, identifier, properties dictionary.
- Code pattern dictionary.
- => ~1.2 bits/code construction.
- => ~2-6 bits/string, identifier, properties use.
Results
- With a good dictionary, ~size parity with minification + brotli.
- Without minification
- Further optimizations coming :)
… and
- Start parsing
init before later is received.
- Start compiling
init before later is received.
- Start running
init before later is received.
- So yeah, we’re working on streaming JavaScript.
Results (lab)
- Time spent parsing: -75% (*).
- Time spent compiling: ~background task (*).
(*) Simulations.
- JavaScript startup is a bottleneck.
- But it doesn’t need to be!
- Reduce the amount of work at every step.
- Improvements in progress.
- Experiments in progress.
- No programming language harmed!
Soon on a browser / Node near you? :)